﻿using System;
using System.Reflection;

namespace DapperDal.Mapper
{
    /// <summary>
    /// 实体属性映射器接口
    /// </summary>
    public interface IPropertyMap
    {
        /// <summary>
        /// 属性名
        /// </summary>
        string Name { get; }

        /// <summary>
        /// 属性对应表字段名
        /// </summary>
        string ColumnName { get; }

        /// <summary>
        /// 属性是否忽略
        /// </summary>
        bool Ignored { get; }

        /// <summary>
        /// 属性是否只读
        /// </summary>
        bool IsReadOnly { get; }

        /// <summary>
        /// 属性类型
        /// </summary>
        KeyType KeyType { get; }

        /// <summary>
        /// 属性元数据访问器
        /// </summary>
        PropertyInfo PropertyInfo { get; }
    }

    /// <summary>
    /// 实体属性映射器类
    /// </summary>
    public class PropertyMap : IPropertyMap
    {
        /// <summary>
        /// 初始化实体属性映射器
        /// </summary>
        /// <param name="propertyInfo">属性元数据访问器</param>
        public PropertyMap(PropertyInfo propertyInfo)
        {
            PropertyInfo = propertyInfo;
            ColumnName = PropertyInfo.Name;
        }

        /// <inheritdoc />
        public string Name
        {
            get { return PropertyInfo.Name; }
        }

        /// <inheritdoc />
        public string ColumnName { get; private set; }

        /// <inheritdoc />
        public KeyType KeyType { get; private set; }

        /// <inheritdoc />
        public bool Ignored { get; private set; }

        /// <inheritdoc />
        public bool IsReadOnly { get; private set; }

        /// <inheritdoc />
        public PropertyInfo PropertyInfo { get; private set; }

        /// <summary>
        /// 设置实体属性和表字段的映射，支持链式调用
        /// </summary>
        /// <param name="columnName">表字段名</param>
        /// <returns>实体属性映射器</returns>
        public PropertyMap Column(string columnName)
        {
            ColumnName = columnName;
            return this;
        }

        /// <summary>
        /// 设置实体属性类型，支持链式调用
        /// </summary>
        /// <param name="keyType">属性类型</param>
        /// <returns>实体属性映射器</returns>
        public PropertyMap Key(KeyType keyType)
        {
            if (Ignored)
            {
                throw new ArgumentException(string.Format("'{0}' is ignored and cannot be made a key field. ", Name));
            }

            if (IsReadOnly)
            {
                throw new ArgumentException(string.Format("'{0}' is readonly and cannot be made a key field. ", Name));
            }

            KeyType = keyType;
            return this;
        }

        /// <summary>
        /// 设置实体属性忽略，支持链式调用
        /// </summary>
        /// <returns>实体属性映射器</returns>
        public PropertyMap Ignore()
        {
            if (KeyType != KeyType.NotAKey)
            {
                throw new ArgumentException(string.Format("'{0}' is a key field and cannot be ignored.", Name));
            }

            Ignored = true;
            return this;
        }

        /// <summary>
        /// 设置实体属性只读，支持链式调用
        /// </summary>
        /// <returns>实体属性映射器</returns>
        public PropertyMap ReadOnly()
        {
            if (KeyType != KeyType.NotAKey)
            {
                throw new ArgumentException(string.Format("'{0}' is a key field and cannot be marked readonly.", Name));
            }

            IsReadOnly = true;
            return this;
        }
    }

    /// <summary>
    /// 属性类型枚举
    /// </summary>
    public enum KeyType
    {
        /// <summary>
        /// 属性不是键，且不自增的
        /// The property is not a key and is not automatically managed.
        /// </summary>
        NotAKey,

        /// <summary>
        /// 属性是自增主键
        /// The property is an integery-based identity generated from the database.
        /// </summary>
        Identity,

        /// <summary>
        /// 属性是触发主键
        /// The property is an identity generated by the database trigger.
        /// </summary>
        TriggerIdentity,

        /// <summary>
        /// 属性是GUID
        /// The property is a Guid identity which is automatically managed.
        /// </summary>
        Guid,

        /// <summary>
        /// 属性是不自增的键
        /// The property is a key that is not automatically managed.
        /// </summary>
        Assigned
    }
}